import EventManager from "../../Script/Common/EventManager";
import { EventType } from "../../Script/GameSpecial/GameEventType";
import TeachStepData from "./TeachStepData";

/**新手教学流程管理器 */
export default class TeachManager {

    /**当前进入的是第几步教学步骤，-1表示还没开始 */
    protected static curTeackStep: number = 0;
    /**所有教学步骤对象 */
    protected static teachStepData: TeachStep[] = [];

    public static init(data: TeachStep[]) {
        this.curTeackStep = -1;
        this.teachStepData = data;
        this.onEvents();
    }
    protected static onEvents() {
        EventManager.on(EventType.Common.TeachEvents.startTeach, this.startTeach, this);
        EventManager.on(EventType.Common.TeachEvents.enterNextTeach, this.enterNextTeachStep, this);
        EventManager.on(EventType.Common.TeachEvents.enterTeachStepById, this.enterTeachStepById, this);
    }
    protected static offEvents() {
        EventManager.off(EventType.Common.TeachEvents.startTeach, this.startTeach, this);
        EventManager.off(EventType.Common.TeachEvents.enterNextTeach, this.enterNextTeachStep, this);
        EventManager.off(EventType.Common.TeachEvents.enterTeachStepById, this.enterTeachStepById, this);
    }

    /**开始教学，进入第一步教学步骤 */
    protected static startTeach() {
        this.curTeackStep = 0;
        let step = this.teachStepData[this.curTeackStep];
        if (!!step) {
            step.enter();
        } else {
            console.error("教学开始，但不存在教学数据！");
            this.teachFinish();
        }
    }
    /**进入下一步教学步骤 */
    protected static enterNextTeachStep() {
        let step = this.teachStepData[this.curTeackStep];
        if (!!step) {
            step.exit();
        }
        this.curTeackStep++;
        if (this.curTeackStep >= this.teachStepData.length) {
            this.teachFinish();
            return;
        }
        step = this.teachStepData[this.curTeackStep];
        if (!!step) {
            step.enter();
        } else {
            console.error("进入下一步教学，但不存在教学步骤:", this.curTeackStep, this.teachStepData);
            this.teachFinish();
        }

    }
    /**进入指定id的教学步骤 */
    protected static enterTeachStepById(id: number) {
        let step = this.teachStepData[this.curTeackStep];
        if (!!step) {
            step.exit();
        }
        let index = -1;
        for (let i = this.teachStepData.length - 1; i >= 0; --i) {
            if (this.teachStepData[i].id == id) {
                index = i;
                break;
            }
        }
        if (index == -1) {
            console.error("不存在指定id的教学步骤：", id, this.teachStepData);
            return;
        }
        this.curTeackStep = index;
        step = this.teachStepData[this.curTeackStep];
        step.enter();
    }
    /**教学完成 */
    protected static teachFinish() {
        EventManager.emit(EventType.Common.TeachEvents.teachFinish);
        this.exitTeach();
    }
    /**退出教学，将注销事件，销毁教学步骤数据 */
    protected static exitTeach() {
        for (let i = this.teachStepData.length - 1; i >= 0; --i) {
            this.teachStepData[i].destroy();
        }
        this.teachStepData = [];
        this.offEvents();
    }

}

/**教学步骤对象 */
export type TeachStep = {
    /**步骤编号，可通过id直接跳转到指定的教学步骤 */
    id: number;
    /**初始化：初始化自身参数，记录游戏中的对象等 */
    init: () => void;
    /**重置自身参数，对象引用等 */
    reset: () => void;
    /**销毁对象：注销事件，释放对象引用等 */
    destroy: () => void;
    /**进入该操作 */
    enter: () => void;
    // /**玩家按教学提示操作成功 */
    // success: () => void;
    /**教学成功，退出步骤：重置对象状态、释放对象引用等 */
    exit: () => void;
    // /**玩家按教学提示操作失败 */
    // fail: () => void;
    // /**教学失败，重新开始：重置自身参数、对象状态等 */
    // restart: () => void;

}

TeachStepData.createTeachLevelData();
TeachManager.init(TeachStepData.getTeachData());